<template>
  <div class="detail-contain">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登陆账户设置" name="first">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            :rules="rules"
            label-width="350px"
            class="demo-ruleForm"
            style="width:40%;margin-top:40px"
          >
            <el-form-item label="姓名：" prop="username">
              <el-input v-model="ruleForm.username" type="text" />
            </el-form-item>
            <el-form-item label="密码：" prop="password">
              <el-input v-model="ruleForm.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="updateInfo('ruleForm')"
              >更新</el-button>
              <el-button
                type="primary"
                @click="$router.push('/employees')"
              >取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <detailInfo />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees'
import { getMoreInfo } from '../../api/user'
import detailInfo from './components/info.vue'
import job from './components/Job.vue'
export default {
  name: 'Detail',
  components: {
    detailInfo,
    job
  },
  data() {
    return {
      form: {},
      activeName: 'first',
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          {
            min: 3,
            max: 5,
            message: '姓名长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          {
            min: 6,
            max: 9,
            message: '密码长度长度在 6 到 9 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  async created() {
    const userInfo = await getMoreInfo({ id: this.$route.params.id })
    // console.log(userInfo)
    this.form = userInfo
    this.ruleForm.username = userInfo.username
  },
  methods: {
    updateInfo(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          await saveUserDetailById({ ...this.form, ...this.ruleForm })
          this.$router.push({ path: '/employees' })
          this.$message.success('更新成功')
        } else {
          this.$message.error('校验失败，请重新输入')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.detail-contain {
  padding: 20px;
}
</style>
